﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;


/* Usage:
 * EmbedBuilder eb = new EmbedBuilder(s);
            eb.width = 640;
            eb.height = 385;
            eb.startTime = 10;
            eb.GetEmbedCode(); <-- returns the code to be embedded
  
*/
namespace t_205_verk_hopur_12.Helpers
{
    public class EmbedBuilder
    {
        private string url;
        
        public int width { get; set; }
        public int height { get; set; }
        public int startTime { get; set; }

        // Constructor. Takes a full embed code and stripst the URL from the code.
        public EmbedBuilder(string s)
        {
            string fullURL = ExtractURL(s);
            string[] urlParts = fullURL.Split('&');
            url = urlParts[0];

            width = 640;
            height = 385;
            startTime = 0;
        }

        // Takes an embed code and uses a regular expression to find the URL and returnes it.
        private string ExtractURL(string txt)
        {
            Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\" +
                "&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

            MatchCollection mactches = regx.Matches(txt);

            foreach (Match match in mactches)
            {
                txt = match.Value;
            }

            return txt;
        }

        // After the properties of the code has been set this function builds the codeto be embedded.
        public string GetEmbedCode()
        {
            return "<object width=\"" + width + "\" height=\"" + height + "\">" +
                    "<param name=\"movie\" value=\"" + url + "&autoplay=1&rel=0&start=" + startTime + "\"></param>" +
                    "<param name=\"allowFullScreen\" value=\"true\"></param>" +
                    "<param name=\"allowScriptAccess\" value=\"always\"></param>" +
                    "<embed src=\"" + url + "&autoplay=1&rel=0&start=" + startTime + "\" type=\"application/x-shockwave-flash\"" +
                    "allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\"" + width + "\" height=\"" + height + "\">" +
                    "</embed></object>";
        }




    }
}
